from keras.layers import Input, Dense
from keras.engine.training import Model
from keras.models import Sequential, model_from_config, Model
from keras import regularizers
import numpy as np


def DNN_auto(x_train):
    encoding_dim = 128  # 编码器维度
    m = x_train.shape[0]
    input_img = Input(shape=(673,))  # 指定输入的维度673/309/378

    # 定义layer层，构建layer函数之间的链式关系
    encoded = Dense(500, activation='relu')(input_img)  # (input layer)
    encoded = Dense(300, activation='relu')(encoded)  # (hidden layer1)
    encoded = Dense(100, activation='relu')(encoded)  # (hidden layer2)
    encoder_output = Dense(encoding_dim)(encoded)  # 128 - output (encoding layer)
    print()
    # decoder layers
    decoded = Dense(100, activation='relu')(encoder_output)
    decoded = Dense(300, activation='relu')(decoded)
    decoded = Dense(500, activation='relu')(decoded)
    decoded = Dense(673, activation='tanh')(decoded)

    # 使用inputs与outputs建立函数链式模型
    autoencoder = Model(input=input_img, output=decoded)  # 将训练和评估添加到网络中
    encoder = Model(input=input_img, output=encoder_output)
    # 训练参数，优化器和损失函数
    autoencoder.compile(optimizer='adam', loss='mse')
    # 训练
    autoencoder.fit(x_train, x_train, epochs=20, batch_size=100, shuffle=True)  # 第二个x_train代替DNN中的训练标签，即在这里，i/p=o/p
    # shuffle：是否在每轮迭代之前混洗数据
    # 预测
    encoded_imgs = encoder.predict(x_train)
    return encoder_output, encoded_imgs  # 返回编码器输出和预测结果
